package com.bulemobi.view.titleView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.jsyh.hcb.R;

/**
 * 模拟actionBar
 */
public class ActionBarView  extends RelativeLayout{
    public ImageButton button_back;
    public TextView title;
    public CheckBox checkBox;
    public ImageButton button_right_icon2,button_right_icon1;
    public Button button_right_text;

    public ActionBarView(Context context) {
        super(context);
        init();
    }

    public ActionBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ActionBarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    protected void init() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View  v = inflater.inflate(R.layout.common_actionbar, this);

        //返回按钮
        button_back = (ImageButton) v.findViewById(R.id.actionbar_left_button);
        button_back.setVisibility(View.VISIBLE);
        button_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity activity = (Activity) getContext();
                activity.finish();
            }
        });

        title = (TextView) v.findViewById(R.id.actionbar_title);
        checkBox = (CheckBox) v.findViewById(R.id.actionbar_center_checkbox);
        button_right_icon2 = (ImageButton) v.findViewById(R.id.actionbar_right_button_icon2);
        button_right_icon1 = (ImageButton)v.findViewById(R.id.actionbar_right_button_icon1);
        button_right_text = (Button) v.findViewById(R.id.actionbar_right_button_text);
    }

    public void setTitleOnClick(OnClickListener listener){
        this.title.setOnClickListener(listener);
        this.checkBox.setOnClickListener(listener);
    }

    public void setTitle(String title) {
        this.title.setText(title);
    }

    public void setTitleIconIsUp(boolean isUp) {
        checkBox.setVisibility(View.VISIBLE);

        if (isUp) {
//            checkBox.setButtonDrawable(R.drawable.icon_title_btn_up);
        }  else {
//            checkBox.setButtonDrawable(R.drawable.icon_title_btn_down);
        }
    }

    /**
     * 最右边的button文本以及点击跳转到的Activity
     * @param buttonText
     * @param classz
     */
    public void setRightButton(String buttonText, final Class<?> classz) {
        this.button_right_text.setVisibility(View.VISIBLE);
        this.button_right_text.setText(buttonText);
        this.button_right_text.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), classz);
                getContext().startActivity(intent);
            }
        });
    }

    public void setRightImage(int imgResId, final Class<?> classz) {
        this.button_right_icon2.setVisibility(View.VISIBLE);
        this.button_right_icon2.setImageBitmap(BitmapFactory.decodeResource(getResources(),imgResId));
        this.button_right_icon2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != classz) {
                    Intent intent = new Intent(getContext(), classz);
                    getContext().startActivity(intent);
                }
            }
        });
    }

    public void setRightImage1(int imgResId, final Class<?> classz) {
        this.button_right_icon1.setVisibility(View.VISIBLE);
        this.button_right_icon1.setImageBitmap(BitmapFactory.decodeResource(getResources(), imgResId));
        this.button_right_icon1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != classz) {
                    Intent intent = new Intent(getContext(), classz);
                    getContext().startActivity(intent);
                }
            }
        });
    }



}